#!groovy

@Library('jenkinslibrary@master') _

//func from shareibrary
def build = new org.devops.build()
def tools = new org.devops.tools()
def deploy = new org.devops.deploy()
def gitlab = new org.devops.gitlab()
def toemail = new org.devops.toemail()


//env
String buildType = "${env.buildType}"
String buildShell = "${env.buildShell}"
String srcUrl = "${env.srcUrl}"
String branchName = "${env.branchName}"
String deployHosts = "${env.deployHosts}"
//String userEmail = "jiaojiao314@qq.com"

// if ("${runOpts}" == "GitlabPush"){
//     branchName = branch - "refs/heads/"
    
//     currentBuild.description = "Trigger'(触发者)'来自by ${userName}'(用户)'${branchName}'(分支)'"
//     //gitlab.ChangeCommitStatus(projectId,commitSha,"running")
//     env.runOpts = "GitlabPush"
    
// } 


//pipeline
pipeline{
    agent { node { label "master"}}

    environment{
        PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/jdk1.8.0_251/bin"
    }
    stages{


        stage("SCM"){

            steps{
                script{ 
                    tools.PrintMes("拉取分支为","green") 

                    println("${branchName}") 
                         
                    tools.PrintMes("获取代码","green") 
                    checkout([$class: 'GitSCM', branches: [[name:"${branchName}"]], 
                                    doGenerateSubmoduleConfigurations: false, 
                                    extensions: [], 
                                    submoduleCfg: [],
                                    userRemoteConfigs: [[credentialsId: 'c043fc31-80c0-473f-a103-c753b6198cda', url: "${srcUrl}"]]])
                }
            }
     
        }


        stage("Build"){
            
            steps{
                
                script{
                
                    tools.PrintMes("执行打包","green")
                    build.Build(buildType,buildShell)

                    tools.PrintMes("执行发布","green")
                    deploy.AnsibleDeploy("${deployHosts}","-m shell -a 'cd /dzzb/usr/local/dzbh-yfd-test/jar-dir && tar zcvf dzbh-yfd-test.jar-`date '+%F-%H-%M-%S'`.tar.gz dzbh-yfd-test.jar' ")
                    deploy.AnsibleDeploy("${deployHosts}",'''-m file -a "path=/dzzb/usr/local/dzbh-yfd-test/jar-dir/dzbh-yfd-test.jar state=absent" ''')     
                    deploy.AnsibleDeploy("${deployHosts}",'''-m synchronize -a "src="${WORKSPACE}"/hzjt-admin/target/hzjt-admin.jar dest=/dzzb/usr/local/dzbh-yfd-test/jar-dir/dzbh-yfd-test.jar archive=yes" ''')

                    deploy.AnsibleDeploy("${deployHosts}","-m shell  -a 'cd /dzzb/usr/local/dzbh-yfd-test/jar-dir && /bin/bash stop-dzbh-yfd.sh' ")
                    deploy.AnsibleDeploy("${deployHosts}","-m shell  -a 'cd /dzzb/usr/local/dzbh-yfd-test/jar-dir && /bin/bash start-dzbh-yfd.sh' ")
                }
            }
        }

    }

     post {
        always{
            script{
                println("always")

                sh 'echo 后端更新成功'
                //toemail.Email("流水线成功,请访问测试页 http://131.2.101.102/hbhx/#/login",userEmail)

            }
        }
    }
        
    //     success{
    //         script{
    //             println("success")
    //             if ("${runOpts}" == "GitlabPush"){
    //                 gitlab.ChangeCommitStatus(projectId,commitSha,"success")
    //             }
    //             toemail.Email("流水线成功",userEmail)
            
    //         }
        
    //     }
    //     failure{
    //         script{
    //             println("failure")
    //             if ("${runOpts}" == "GitlabPush"){
    //                 gitlab.ChangeCommitStatus(projectId,commitSha,"failed")
    //             }
    //             toemail.Email("流水线失败了！",userEmail)
    //         }
    //     }
        
    //     aborted{
    //         script{
    //             println("aborted")
    //             if ("${runOpts}" == "GitlabPush"){
    //                 gitlab.ChangeCommitStatus(projectId,commitSha,"canceled")
    //             }
    //            toemail.Email("流水线被取消了！",userEmail)
    //         }
        
    //     }
    
    // }   
}